import java.util.HashMap;
import java.util.LinkedList;


public class Cons extends Expr {
	private Expr e1;
	private Expr e2;
	
	public Cons(Expr e1, Expr e2) {
		this.e1 = e1;
		this.e2 = e2;
	}

    public Element eval(HashMap<String, Element> nametable, HashMap<String, Proc> functiontable, LinkedList var) {
    	Element car = e1.eval(nametable, functiontable, var);
    	Element cdr = e2.eval(nametable, functiontable, var);
    	
    	if (!cdr.isList()) {
    		throw new IllegalStateException("Expression does not evaluate to a list.");
    	}
    	
    	Element cdrClone = cdr.clone();
    	cdrClone.getListValue().getSequence().add(car.clone());
    	
        return cdrClone;
    }
}
